﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using ABMath.ModelFramework.Models;
using MathNet.Numerics.LinearAlgebra;

namespace ExtraControlLibrary
{
    public partial class ParameterEditor : Form
    {
        public ParameterEditor(List<string> parmNames, List<double> parmValues, List<Model.ParameterState> parmStates)
        {
            InitializeComponent();
            for (int i = 0; i < parmNames.Count; ++i)
                dataGridView.Rows.Add(new object[] { parmNames[i], parmValues[i], parmStates[i].ToString() });
        }

        public Vector GetResultVector()
        {
            var v = new Vector(dataGridView.Rows.Count);
            for (int i = 0; i < v.Length; ++i)
            {
                string vs = dataGridView.Rows[i].Cells[1].Value.ToString();
                v[i] = double.Parse(vs);
            }
            return v;
        }

        public Model.ParameterState[] GetResultStates()
        {
            var pstates = new Model.ParameterState[dataGridView.Rows.Count];
            for (int i = 0; i < dataGridView.Rows.Count; ++i)
                pstates[i] =
                    (Model.ParameterState)
                    Enum.Parse(typeof(Model.ParameterState), dataGridView.Rows[i].Cells[2].Value.ToString());
            return pstates;
        }

        private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            // Validate the CompanyName entry by disallowing empty strings.
            switch (dataGridView.Columns[e.ColumnIndex].Name)

            {
                case "Value":
                    var s = e.FormattedValue.ToString();
                    dataGridView.Rows[e.RowIndex].ErrorText = null;
                    try
                    {
                        var x = double.Parse(s);
                    }
                    catch (Exception)
                    {
                        dataGridView.Rows[e.RowIndex].ErrorText = "Value must be a floating point number.";
                        e.Cancel = true;
                    }
                    break;
                default:
                    break;
            }
        }
    }
}